package com.module.admin.tts.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.module.admin.BaseController;
import com.module.admin.sys.pojo.SysUser;
import com.module.admin.tts.enums.ProjectSign;
import com.module.admin.tts.enums.ServInfoStatus;
import com.ms.client.task.api.pojo.ServInfo;
import com.ms.client.task.api.pojo.TaskProject;
import com.ms.client.task.api.service.ApiServInfoService;
import com.ms.client.task.api.service.ApiTaskJobService;
import com.ms.client.task.api.service.ApiTaskProjectService;
import com.ms.msclient.MsClientResponse;
import com.system.comm.model.Page;
import com.system.comm.utils.FrameJsonUtil;
import com.system.comm.utils.FrameMapUtil;
import com.system.comm.utils.FrameStringUtil;

/**
 * task_project的Controller
 * @author yuejing
 * @date 2015-03-30 14:07:28
 * @version V1.0.0
 */
@Controller
public class TtsTaskProjectController extends BaseController {

	@Autowired
	private ApiTaskJobService apiTaskJobService;
	@Autowired
	private ApiTaskProjectService apiTaskProjectService;
	@Autowired
	private ApiServInfoService apiServInfoService;

	/**
	 * 跳转到管理页
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/ttsTaskProject/f-view/manager")
	public String manger(HttpServletRequest request, ModelMap modelMap) {
		List<ServInfo> servInfos = apiServInfoService.findByStatus(ServInfoStatus.NORMAL.getCode()).getBody();
		modelMap.put("servInfos", servInfos);
		return "admin/tts/task/project-manager";
	}

	@RequestMapping(value = "/ttsTaskProject/f-view/chart")
	public String chart(HttpServletRequest request, ModelMap modelMap) {
		List<Map<String, Object>> projects = apiTaskJobService.findProjectidCount().getBody();
		Map<Integer, String> projectnames = new HashMap<Integer, String>();
		for (Map<String, Object> map : projects) {
			Integer projectid = FrameMapUtil.getInteger(map, "projectid");
			String projectname = FrameMapUtil.getString(projectnames, projectid.toString());
			if(FrameStringUtil.isEmpty(projectname)) {
				TaskProject taskProject = apiTaskProjectService.get(projectid).getBody();
				//Map<String, Object> projectMap = ttsTaskProjectService.get(projectid);
				projectname = taskProject.getName();//FrameMapUtil.getString(projectMap, "name");
				projectnames.put(projectid, projectname);
			}
			map.put("projectname", projectname);
		}
		modelMap.put("projectsJson", FrameJsonUtil.toString(projects));
		return "admin/tts/task/project-chart";
	}

	/**
	 * 跳转到编辑页[包含新增和编辑]
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/ttsTaskProject/f-view/edit")
	public String edit(HttpServletRequest request, ModelMap modelMap, Integer id) {
		if(id != null) {
			TaskProject taskProject = apiTaskProjectService.get(id).getBody();
			//Map<String, Object> taskProject = ttsTaskProjectService.get(id);
			modelMap.put("taskProject", taskProject);
		}
		String projectSigns = FrameJsonUtil.toString(ProjectSign.getList());
		modelMap.put("projectSigns", projectSigns);
		return "admin/tts/task/project-edit";
	}
	/**
	 * 分页获取信息
	 * @return
	 */
	@RequestMapping(value = "/ttsTaskProject/f-json/pageQuery")
	@ResponseBody
	public void pageQuery(HttpServletRequest request, HttpServletResponse response, TaskProject taskProject) {
		MsClientResponse<Page<TaskProject>> frame = apiTaskProjectService.pageQuery(taskProject);
		writerJson(response, frame);
	}
	@RequestMapping(value = "/ttsTaskProject/f-json/save")
	@ResponseBody
	public void save(HttpServletRequest request, HttpServletResponse response, TaskProject taskProject) {
		SysUser loginUser = getSessionUser(request);
		taskProject.setAdduser(loginUser.getUserId());
		MsClientResponse<TaskProject> frame = apiTaskProjectService.save(taskProject);
		writerJson(response, frame);
	}
	@RequestMapping(value = "/ttsTaskProject/f-json/delete")
	@ResponseBody
	public void delete(HttpServletRequest request, HttpServletResponse response, Integer id) {
		MsClientResponse<TaskProject> frame = apiTaskProjectService.delete(id);
		writerJson(response, frame);
	}
}